package com.zust.ysc012.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

import static com.zust.ysc012.service.EnterService.getUserName;

@WebServlet(urlPatterns = "/jumpingServlet")
public class JumpingServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String aim = req.getParameter("aim");
            HttpSession session = req.getSession(true);
            int i = (int) session.getAttribute("sign");
            String account = (String) session.getAttribute("account");
            String name = getUserName(account);
            session.setAttribute("username", name);
            if (aim.equals("1")) {
                req.getRequestDispatcher("/WEB-INF/blank.jsp").forward(req, resp);
            } else if (aim.equals("2")) {
                req.getRequestDispatcher("/WEB-INF/notice.jsp").forward(req, resp);
            } else if (aim.equals("3")) {
                req.getRequestDispatcher("/WEB-INF/research.jsp").forward(req, resp);
            } else if (aim.equals("4")) {
                if (i == 1) {

                    req.getRequestDispatcher("/WEB-INF/student/table.jsp").forward(req, resp);
                } else {

                    req.getRequestDispatcher("/WEB-INF/teacher/table.jsp").forward(req, resp);
                }
            } else if (aim.equals("5")) {
                if (i == 1) {
                    req.getRequestDispatcher("/WEB-INF/student/uploaded.jsp").forward(req, resp);
                } else {
                    req.getRequestDispatcher("/WEB-INF/teacher/uploaded.jsp").forward(req, resp);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


}
